package org.goyo.main.services.sale.impl;

import java.math.BigDecimal;
import java.util.List;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import org.goyo.common.exception.DBException;
import org.goyo.common.exception.ServiceException;
import org.goyo.main.domain.dao.sale.IReceiptDao;
import org.goyo.main.domain.dao.sale.ISaleOrderDao;
import org.goyo.main.domain.entity.sale.Receipt;
import org.goyo.main.services.BaseService;
import org.goyo.main.services.sale.IReceiptService;
import org.springframework.stereotype.Service;

@Service
@Transactional
public class ReceiptServiceImpl  extends BaseService  implements  IReceiptService{
	@Resource
	private IReceiptDao receiptDao;
	@Resource
	private ISaleOrderDao saleOrderDao;

	@Override
	public BigDecimal saveOrUpdateInfo(List<Receipt> gridList, Object[] uuids)	throws DBException, ServiceException {
		BigDecimal amount = new BigDecimal(0);
		String parentId = null;
		for(Receipt receipt : gridList){
			if(parentId == null)
				parentId = receipt.getParentId();
			receiptDao.saveOrUpdate(receipt);
			//删除数据
			if(uuids != null && uuids[0] != null){
				receiptDao.delBatch(Receipt.class, "uuid", uuids);
			}
			amount = amount.add(receipt.getReceiptAmout());
		}
		saleOrderDao.updateSumReceiptAmount(amount, parentId);
		return amount;
	}

	@Override
	public List<Receipt> findByParentId(String parentId) throws DBException,
			ServiceException {
		return receiptDao.findByParentId(parentId);
	}

}
